package cn.net.mugui.net.pc.message;

import com.mugui.Mugui;
import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;
import com.mugui.base.client.net.baghandle.NetHandle;
import com.mugui.base.client.net.base.Module;
import com.mugui.base.client.net.bean.Message;
import com.mugui.base.client.net.bean.NetBag;

import cn.net.mugui.net.p2p.ServerAttr;
import cn.net.mugui.net.p2p.ServerManager;
import cn.net.mugui.net.pc.panel.ChatPanel;

@Module(name = "sucess", type = "method")
@Component
public class SucessMsg implements Mugui {

	@Autowired
	ChatPanel chatPanel;
	@Autowired
	private ServerManager manager;
	
	public Message base(NetBag bag) {
		ServerAttr serverAttr = manager.get(bag.getSession());
		if (serverAttr == null) {
			// 收到一个陌生用户的消息
			serverAttr = new ServerAttr().setStatus(ServerAttr.status_1);
			serverAttr.setType(ServerAttr.type_0);
			serverAttr.setAddress(bag.getSession());
			serverAttr.setName(serverAttr.getAddress());
			NetBag netBag = NetHandle.netBagLocal.get();

			serverAttr.setHost(netBag.getFrom_host());
			serverAttr.setPort(netBag.getFrom_port());
			manager.add(serverAttr.getAddress(), serverAttr);
			chatPanel.addUserView(serverAttr);
		}
		chatPanel.addChatMsg(serverAttr,bag.getData());
		return Message.ok();
	}
}
